import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';

import './app.dart';
import './public.dart';

void main() async {
  LogUtil.v('启动app...');
  WidgetsFlutterBinding.ensureInitialized();

  await initState();

  initGlobalException();
  initStyle();
  // runApp(MyApp());
  runZoned(() {
    runApp(MyApp());
  }, zoneSpecification: ZoneSpecification(
      print: (Zone self, ZoneDelegate parent, Zone zone, String line) {
    parent.print(self, "$line");
  }), onError: (Object obj, StackTrace stack) {
    /// 异步错误
    console(obj, l1: stack);
  });
}

/// 前置初始化
initState() async {
  await SpUtil.getInstance();
  setDesignWHD(375, 667, density: 1);
  LogUtil.init(tag: '', isDebug: true);
}

/// 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
initStyle() {
  if (Platform.isAndroid) {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        systemNavigationBarColor: Colors.white));
  }
}

/// 全局页面报错
initGlobalException() {
  ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
    console(flutterErrorDetails);
    return new Center(child: new Text("App错误，快去反馈吖!"));
  };

  /// 全局错误
  FlutterError.onError = (FlutterErrorDetails details) async {
    console(details);
  };
}
